using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Appr;
using net_work.IService.Appr;

namespace net_work.Controllers.Appr;

[Route("api/[controller]")]
[ApiController]
public class FlowController(ILogger<FlowController> logger, IFlowService flowService) : ControllerBase
{
    /// <summary>
    /// 获取流程列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListFlows([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取流程列表");
        var result = await flowService.GetFlowsPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取流程信息
    /// </summary>
    /// <param name="flowId">流程ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetFlowById([FromQuery] long flowId)
    {
        logger.LogInformation("获取流程信息");
        return await flowService.SelectByIdAsync(flowId) is { } flow
            ? AjaxResult.Success(flow)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增流程
    /// </summary>
    /// <param name="flowAddRequest">新增流程实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> AddFlow([FromBody] FlowAddRequest flowAddRequest)
    {
        logger.LogInformation("新增流程");
        var flowId = await flowService.InsertFlow(flowAddRequest);
        return flowId > 0
            ? AjaxResult.Success(new { flowId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改流程
    /// </summary>
    /// <param name="flowUpdateRequest">修改流程实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> UpdateFlow([FromBody] FlowUpdateRequest flowUpdateRequest)
    {
        logger.LogInformation("修改流程");
        return await flowService.UpdateFlow(flowUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除流程
    /// </summary>
    /// <param name="ids">流程ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> DeleteFlow([FromBody] List<long> ids)
    {
        logger.LogInformation("删除流程");
        return await flowService.DeleteFlowByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 发布流程
    /// </summary>
    /// <param name="flowId">流程ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("release")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> ReleaseFlow([FromBody] long flowId)
    {
        logger.LogInformation("发布流程");
        return await flowService.ReleaseFlow(flowId) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}